//
//  PluginComponentController.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/11/4.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "PluginComponentController.h"
#import "PaymentHelper.h"
#import "WXApi.h"

@interface PluginComponentController ()<CWAlipayDelegate,WXApiDelegate>

@property (nonatomic, strong) NSString *webViewUrl;
@property (nonatomic, assign) float webViewWidth;
@property (nonatomic, assign) float webViewHeight;
@property (nonatomic, strong) NSString *path;
@property (nonatomic, strong) NSString *rule;

@end

@implementation PluginComponentController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view addSubview:self.webView];

}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    
}

- (UIWebView *)webView {
    
    if (!_webView) {
        
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, _webViewWidth, _webViewHeight)];
        _webView.delegate = self;
        _webView.scrollView.delaysContentTouches = YES;
        _webView.scrollView.scrollEnabled = NO;
        _webView.scrollView.scrollsToTop = NO;
        
    }
    
    return _webView;
}

/**
 *  重新调整高度
 *
 *  @param frame           新的frame
 *  @param path            加载url
 *  @param rule            参数：rule,拥有回调js时使用
 *  @param finishLoadBlock callBack
 */
- (void)reinitWithFrame:(CGRect)frame path:(NSString *)path rule:(NSString *)rule finishLoadBlock:(FinishLoadBlock)finishLoadBlock  {
    
    _path = path;
    _webViewHeight = frame.size.height;
    _webViewWidth = frame.size.width;
    _finishLoadBlock = finishLoadBlock;
    _rule = rule;
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame = frame;
    self.webView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    
    [self initWebViewDataWithPath:path];
    
}

- (void)initWebViewDataWithPath:(NSString *)path {
    
    NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    [self.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:path]];
    
}

#pragma mark - private methord

- (void)loadDocument:(NSString*)docName {
    
    NSString *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];
    NSString *path = [mainBundleDirectory  stringByAppendingPathComponent:docName];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

#pragma mark –
#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    if ( [request.mainDocumentURL.relativePath isEqualToString:@"/click/true"] ) {
        
        return false;
    }
    return true;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    NSLog(@"title11=%@",title);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.clientHeight;"] floatValue];
//
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    
    if (_finishLoadBlock) {
        _finishLoadBlock(fittingSize);
    }
    
    if (!IsEmpty(self.rule)) {
        [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"acceptInputParameter('%@')",self.rule]];
    }
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%@",error.description);
}

#pragma mark- CWAlipayDelegate

- (void)alipayCompleteCallback:(NSDictionary *)resultDic {
    
    
}

#pragma mark- WXApiDelegate

- (void)onResp:(BaseResp*)resp
{
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp *)resp;
        switch (response.errCode) {
            case WXSuccess:
            
                
            break;
                
            default:
            
            break;
        }
    }
    
}

@end
